/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <journal/models/db/common_table.h>

namespace journal { namespace services { namespace settings { namespace db {

namespace ct = journal::model::db;
namespace s = baja::storage;

class setting_group_table : public ct::icommon_table_client
{
public:
    setting_group_table();
    static std::shared_ptr<setting_group_table> create(std::shared_ptr<s::iengine> dbEngine);

    // ibase_table
    std::shared_ptr<ct::icommon_table> common();

    // icommon_table_client
    void defineColumns(std::shared_ptr<s::itable> table);
    void defineIndexColumns(std::shared_ptr<s::iindex> primaryKeyIndex);
    void defineRowCells(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> row);

    void write(
        std::wstring name,
        s::binary_blob& inBlob);

    bool read(
        std::wstring name,
        s::binary_blob& outBlob);

    void setting_group_table::readAll(
        std::map<std::wstring, s::binary_blob>& outResult);

private:
    std::shared_ptr<ct::icommon_table> _commonTable;
    std::weak_ptr<ct::icommon_table_client> _commonTableClient;

    std::shared_ptr<s::icolumn_typed<std::wstring>> _nameColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _settingsBlobSizeColumn;
    std::shared_ptr<s::icolumn_typed<s::binary_blob>> _settingsBlobColumn;

    std::shared_ptr<s::icell_typed<std::wstring>> _nameCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _settingsBlobSizeCell;
    std::shared_ptr<s::icell_typed<s::binary_blob>> _settingsBlobCell;

    std::shared_ptr<s::iindex_column> _idIndexColumn;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _settingsBlobCells;

    static const b::uint32 nameSizeInBytes;

    std::shared_ptr<std::vector<std::shared_ptr<s::iresult_row>>> findRow(std::wstring name);
};

}}}} // journal::services::settings::db

